package com.fs.sale.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.util.page.PageUtils;
import com.fs.sale.pojo.SaleOrder;
import com.fs.sale.service.ObdDeliveryService;
import com.fs.sale.service.SaleOrderService;
import com.fs.sale.vo.ObdDeliveryVo;
import com.fs.sale.vo.OrderSearch;
import com.fs.sale.vo.SaleOrderItem;
import com.fs.sale.vo.SaleOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("saleOrder")
public class SaleOrderController extends BaseController {
    @Autowired
    SaleOrderService saleOrderService;


    @PostMapping("select")
    public TableDataInfo select(@RequestBody OrderSearch orderSearch){
        PageDomain pageDomain= PageUtils.startPage();
        if(Objects.nonNull(orderSearch.getCurrentPage()))
            pageDomain.setPageNum(orderSearch.getCurrentPage());
        if(Objects.nonNull(orderSearch.getPageSize()))
            pageDomain.setPageSize(orderSearch.getPageSize());


        Page<SaleOrderVo> page=saleOrderService.select(pageDomain,orderSearch);

        return this.getDataTable(page);
    }

    @PostMapping("add")
    public AjaxResult insert(@RequestBody SaleOrder saleOrder){
        saleOrderService.add(saleOrder);
//        System.out.println("输出一下："+saleOrder);
        return  AjaxResult.success();
    }

    @GetMapping("toExamine")
    public AjaxResult toExamine(@RequestParam(value = "orderId",required = false)Integer orderId,@RequestParam(value = "newStatus",required = false)Integer newStatus){
        if(saleOrderService.toExamine(orderId,newStatus)>0){
            return AjaxResult.success();
        }else if(saleOrderService.toExamine(orderId,newStatus)==0){
            return AjaxResult.error("不具备权限");
        }
            return AjaxResult.error();
    }

    @GetMapping("SaleOrderItem")
    public AjaxResult SaleOrderItem(Integer orderId){
      List<SaleOrderItem> saleOrderItems=  saleOrderService.selectDetailedById(orderId);
      AjaxResult ajaxResult=new AjaxResult();
      ajaxResult.put("saleOrderItems",saleOrderItems);
      return ajaxResult;
    }

}
